package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.request.AccessTokenRequest;
import sz.szsmk.citizencard.model.network.bean.request.AuthUserReq;
import sz.szsmk.citizencard.model.network.bean.request.CardnoAuthorizationReq;
import sz.szsmk.citizencard.model.network.bean.request.GetOrderOpenid;
import sz.szsmk.citizencard.model.network.bean.request.QueryAllCardsReq;
import sz.szsmk.citizencard.model.network.bean.request.UploadPic;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CardnoAuthorizationRes;
import sz.szsmk.citizencard.model.network.bean.response.GetOrderOpenidRes;
import sz.szsmk.citizencard.model.network.bean.response.QuerLeisureCardsInfoRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryLeisureCardRes;
import sz.szsmk.citizencard.model.network.bean.response.SeleciUserAuthRes;
import sz.szsmk.citizencard.mvp.ApplicationAuthorMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

public class ApplicationAuthorPresenter extends AppBasePresenter<ApplicationAuthorMvpView> {
    NetworkDateSources mNetworkDateSource;

    @Inject
    public ApplicationAuthorPresenter() {
        getComponent().inject(this);
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    public void applicationAuthor(final String type) {
        AuthUserReq authUserReq = new AuthUserReq();
        /*1.社保 2.公积金 3 桂花分*/

        authUserReq.setType(type);
        mNetworkDateSource.authUser(authUserReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                //社保
                if (type.equals("1")) {
                    //0未授权，1已授权，2未实名(L3认证)
                    Constant.SB_AUTH = "1";
                }
                //公积金
                if (type.equals("2")) {
                    Constant.GJJ_AUTH = "1";
                }
                //桂花分
                if (type.equals("3")) {
                    Constant.GHF_AUTH = "1";
                }

                getMvpView().authSucc(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                getMvpView().authFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void quryLeisureCards() {
        QueryAllCardsReq queryAllCardsReq = new QueryAllCardsReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        queryAllCardsReq.setAccess_token(access_token);
        String json = new Gson().toJson(queryAllCardsReq);
        mNetworkDateSource.quryLeisureCards(queryAllCardsReq, new SubscribeResult<QuerLeisureCardsInfoRes>() {
            @Override
            protected void onOk(QuerLeisureCardsInfoRes response) {
                getMvpView().queryLeisureSucc(response);
            }

            @Override
            protected void onFailure(QuerLeisureCardsInfoRes response) {
                super.onFailure(response);
                getMvpView().queryLeisureFail(response);

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }


    public void cardnoAuthorization(String type, String aCard) {
        CardnoAuthorizationReq cardnoAuthorizationReq = new CardnoAuthorizationReq();
        cardnoAuthorizationReq.setCard_no(aCard);
        //休闲年卡1 文化消费2 体育惠民3
        cardnoAuthorizationReq.setType(type);
        String json = new Gson().toJson(cardnoAuthorizationReq);
        mNetworkDateSource.cardnoAuthorization(cardnoAuthorizationReq, new SubscribeResult<CardnoAuthorizationRes>() {
            @Override
            protected void onOk(CardnoAuthorizationRes response) {
                String json1 = new Gson().toJson(response);
                getMvpView().cardnoAuthorizationSucc(response);
            }

            @Override
            protected void onFailure(CardnoAuthorizationRes response) {
                super.onFailure(response);
                getMvpView().cardnoAuthorizationFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }


    public void selectUserAuth(final String type) {
        AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
        mNetworkDateSource.selectUserAuth(accessTokenRequest, new SubscribeResult<SeleciUserAuthRes>() {
            @Override
            protected void onOk(SeleciUserAuthRes response) {

                getMvpView().selectUserAuthSucc(response, type);
            }

            @Override
            protected void onFailure(SeleciUserAuthRes response) {
                super.onFailure(response);
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    public void getGetOrderId(final String type, final String url) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("HUIMIN");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                Log.d("clark","token="+toJson);
                Constant.EXTENT = response.getExtend();
                getMvpView().getGetOrderIdSucc(response, type, url);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {
                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    public void upLoadPic(String cardNo, String photo){
        UploadPic uploadPic = new UploadPic();
        uploadPic.setCardNo(cardNo);
        uploadPic.setPhoto(photo);

        mNetworkDateSource.upLoadPic(uploadPic, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                getMvpView().getUploadPic();
            }

            @Override
            protected void onNext(BaseBean response) {
                super.onNext(response);
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }
}
